package com.hnu.highlynewapp.service.impl;

import com.hnu.highlynewapp.entity.ContactEntity;
import com.hnu.highlynewapp.entity.FinancialInstitutionInfoEntity;
import com.hnu.highlynewapp.entity.UserEntity;
import com.hnu.highlynewapp.enums.StatusEnum;
import com.hnu.highlynewapp.service.ContactService;
import com.hnu.highlynewapp.service.UserService;
import com.hnu.highlynewapp.vo.ServiceInstitutionInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hnu.highlynewapp.utils.PageUtils;
import com.hnu.highlynewapp.utils.Query;

import com.hnu.highlynewapp.dao.ServiceInstitutionInfoDao;
import com.hnu.highlynewapp.entity.ServiceInstitutionInfoEntity;
import com.hnu.highlynewapp.service.ServiceInstitutionInfoService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service("serviceInstitutionInfoService")
public class ServiceInstitutionInfoServiceImpl extends ServiceImpl<ServiceInstitutionInfoDao, ServiceInstitutionInfoEntity> implements ServiceInstitutionInfoService {

    @Resource
    private UserService userService;

    @Resource
    private ContactService contactService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ServiceInstitutionInfoEntity> page = this.page(
                new Query<ServiceInstitutionInfoEntity>().getPage(params),
                new QueryWrapper<ServiceInstitutionInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<ServiceInstitutionInfoEntity> getListByParma(String serviceType) {
        QueryWrapper<ServiceInstitutionInfoEntity> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(serviceType)) {
            wrapper.eq("service_type", serviceType);
        }

        List<ServiceInstitutionInfoEntity> list = baseMapper.selectList(wrapper);
        return list;

    }
    @Override
    public List<ServiceInstitutionInfoEntity> getNoCheckList() {
        List<ServiceInstitutionInfoEntity> list=baseMapper.selectList(new QueryWrapper<ServiceInstitutionInfoEntity>().eq("status", StatusEnum.STATU_ONE.getCode()));
        return list;
    }

    @Override
    @Transactional
    public void auth(ServiceInstitutionInfoVo serviceInstitutionInfoVo) {
        ServiceInstitutionInfoEntity serviceInstitutionInfoEntity=new ServiceInstitutionInfoEntity();

        ContactEntity contactEntity = new ContactEntity();
        BeanUtils.copyProperties(serviceInstitutionInfoVo, serviceInstitutionInfoEntity);
        BeanUtils.copyProperties(serviceInstitutionInfoVo, contactEntity);

        contactEntity.setAddress(serviceInstitutionInfoVo.getContactAddress());
        contactEntity.setEmail(serviceInstitutionInfoVo.getContactEmail());
        contactEntity.setName(serviceInstitutionInfoVo.getContactName());
        contactEntity.setPosition(serviceInstitutionInfoVo.getContactPosition());
        contactEntity.setPhone(serviceInstitutionInfoVo.getContactPhone());
        Long i = contactService.saveAndCheckExist(contactEntity);
        serviceInstitutionInfoEntity.setContactId(i);
        serviceInstitutionInfoEntity.setStatus(StatusEnum.STATU_ONE.getCode());
        this.save(serviceInstitutionInfoEntity);
        UserEntity user = new UserEntity();
        user.setUserId(serviceInstitutionInfoVo.getUserId());
        user.setStatus(1);
        userService.updateById(user);
    }

    @Override
    @Transactional
    public void passCheck(Long serviceInstitutionId) {
        ServiceInstitutionInfoEntity serviceInstitutionInfoEntity=this.getById(serviceInstitutionId);
        serviceInstitutionInfoEntity.setStatus(StatusEnum.STATU_TWO.getCode());
        this.updateById(serviceInstitutionInfoEntity);
        UserEntity userEntity = new UserEntity();
        userEntity.setUserId(serviceInstitutionInfoEntity.getUserId());
        userEntity.setType(5);
        userEntity.setStatus(2);
        userService.updateById(userEntity);
    }

    @Override
    @Transactional
    public void failCheck(Long serviceInstitutionId) {
        ServiceInstitutionInfoEntity serviceInstitutionInfoEntity = this.getById(serviceInstitutionId);
        serviceInstitutionInfoEntity.setStatus(StatusEnum.STATU_THREE.getCode());
        this.updateById(serviceInstitutionInfoEntity);
        UserEntity userEntity = new UserEntity();
        userEntity.setUserId(serviceInstitutionInfoEntity.getUserId());
        userEntity.setStatus(0);
        userService.updateById(userEntity);
    }


}